﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace CMS
{
    public partial class happy : System.Web.UI.Page
    {
        public SqlHelper sqlHelper = null;

        public FenYe fenye = new FenYe();

        string url = "happy.aspx";

        public SqlDataReader dr = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取用户名
            if (Session["stu_no"] != null)
            {
                this.stu_no.InnerText = Session["stu_no"].ToString();
            }
            //else
            //{
            //    Response.Redirect("log.aspx");
            //}
            if (Session["stu_name"] != null)
            {
                this.stu_name.InnerText = Session["stu_name"].ToString();
            }

            if (Request.QueryString["pageNow"] != null)
            {
                fenye.pageNow = int.Parse(Request.QueryString["pageNow"].ToString());
            }

            sqlHelper = new SqlHelper();

            fenye.pageSize = 4;

            string pageNow = fenye.pageNow.ToString();
            string pageSize = fenye.pageSize.ToString();
            string notFound = ((fenye.pageNow - 1) * fenye.pageSize).ToString();

            string sql1 = "SELECT   COUNT(*) FROM happy";
            StringBuilder sb = new StringBuilder(1024);
            sb.AppendFormat("SELECT   TOP ({0}) * ", pageSize);
            sb.Append("FROM      happy ");
            sb.Append("WHERE   (happy_id NOT IN ");
            sb.AppendFormat("                    (SELECT   TOP ({0}) happy_id ", notFound);
            sb.Append("                     FROM      happy AS happy_1 order by happy_id desc)) order by happy_id desc");

            sqlHelper.fenye_query(sql1, sb.ToString(), url, fenye);

            dr = fenye.dr;

        }


        ///   <summary>   
        ///   将指定字符串按指定长度进行截取并加上指定的后缀  
        ///   </summary>   
        ///   <param   name= "oldStr "> 需要截断的字符串 </param>   
        ///   <param   name= "maxLength "> 字符串的最大长度 </param>   
        ///   <param   name= "endWith "> 超过长度的后缀 </param>   
        ///   <returns> 如果超过长度，返回截断后的新字符串加上后缀，否则，返回原字符串 </returns>   
        //public static string StringTruncat(string oldStr, int maxLength, string endWith)
        //{
        //    //判断原字符串是否为空  
        //    if (string.IsNullOrEmpty(oldStr))
        //        return oldStr + endWith;


        //    //返回字符串的长度必须大于1  
        //    if (maxLength < 1)
        //        throw new Exception("返回的字符串长度必须大于[0] ");


        //    //判断原字符串是否大于最大长度  
        //    if (oldStr.Length > maxLength)
        //    {
        //        //截取原字符串  
        //        string strTmp = oldStr.Substring(0, maxLength);


        //        //判断后缀是否为空  
        //        if (string.IsNullOrEmpty(endWith))
        //            return strTmp;
        //        else
        //            return strTmp + endWith;
        //    }
        //    return oldStr;
        //}   

        protected void send_Click(object sender, EventArgs e)
        {
            string content = "";
            if (this.tbcontent.Text == "")
            {
                Response.Write("<script>alert('请输入内容')</script>");
                return;
            }
            else if (this.tbcontent.Text.Length > 100)
            {
                Response.Write("<script>alert('内容不能超过100字')</script>");
                return;
            }
            else
            {
                content = this.tbcontent.Text;
            }
            sqlHelper = new SqlHelper();

            string type = this.ddltype.Text;

            DateTime dt = System.DateTime.Now;
            string datetime = dt.ToString("yyyy-MM-dd HH:mm:ss");

            string sql = "INSERT INTO happy(happy_publisher, happy_type, happy_publish_time, happy_content) VALUES   (N'" + Session["stu_name"].ToString() + "', N'" + type + "', N'" + datetime + "', N'" + content + "')";
            

            if (sqlHelper.ExecuteNonQuery(sql) > 0)
            {
                Response.Write("<script>alert('发送成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('发送失败');</script>");
            }


            
        }

    }
}